package com.typy.www.tpay.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;

import com.typy.www.tpay.R;

/**
 * Created by 13799 on 2018/7/19.
 */

public class PopupUtil {
    private static PopupUtil instance;
    private Context context;
    private PopupWindow mPopupWindow;
/* 使用 PopupUtil.getDefaul().showPopup(view, "hahah", "hehehe", "", new PopupUtil.PopupListener() {
                                @Override
                                public void clickOne() {
                                    Logutil.print("1111");
                                }

                                @Override
                                public void clickTow() {
                                    Logutil.print("2222");

                                }

                                @Override
                                public void clickCancle() {
                                    Logutil.print("3333");

                                }
                            });*/
    public interface PopupListener{
        void clickOne();
        void clickTow();
        void clickCancle();
    }
    public static void init(Context context){
        if(instance==null)
            instance=new PopupUtil(context);
    }

    public PopupUtil(Context context){
        this.context=context;
    }
    public static PopupUtil  getDefaul(){
        return instance;
    }

    public  void showPopup(View view,String one,String two,String three, final PopupListener listener) {
        View contentView = LayoutInflater.from(context).inflate(
                R.layout.photo_action_selector, null);
        View btnTakePhoto = contentView.findViewById(R.id.btn_take_photo);
        View btnSelect = contentView.findViewById(R.id.btn_select_from_album);
        View btnCancel = contentView.findViewById(R.id.btn_cancel);
        UpdataUiUtil.getDefaul().setText(one,(Button) btnTakePhoto).setText(two,(Button) btnSelect);
        if(!StringUtil.isEmpty(three))
            UpdataUiUtil.getDefaul().setText(three,(Button) btnCancel);
        btnTakePhoto.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mPopupWindow.dismiss();
                listener.clickOne();
            }

        });

        btnSelect.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mPopupWindow.dismiss();
                listener.clickTow();
            }
        });

        btnCancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mPopupWindow.dismiss();
                listener.clickCancle();
            }
        });

        mPopupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT, true);
        mPopupWindow.setTouchable(true);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable(context
                .getResources(), (Bitmap) null));
        mPopupWindow.showAtLocation(view, Gravity.NO_GRAVITY, 0, 0);
    }
    public static  void showPopup(Context context ,PopupWindow mPopupWindow,View contentView,String one,String two,String three, final PopupListener listener) {
        View btnTakePhoto = contentView.findViewById(R.id.btn_take_photo);
        View btnSelect = contentView.findViewById(R.id.btn_select_from_album);
        View btnCancel = contentView.findViewById(R.id.btn_cancel);
            UpdataUiUtil.getDefaul().setText(one,(Button) btnTakePhoto).setText(two,(Button) btnSelect);
            if(two.equals("撤销订单"))
                ((Button) btnSelect).setTextColor(context.getResources().getColor(R.color.text_moneyred));
        if(!StringUtil.isEmpty(three))
            UpdataUiUtil.getDefaul().setText(three,(Button) btnCancel);
        btnTakePhoto.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                listener.clickOne();
            }
        });

        btnSelect.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                listener.clickTow();
            }
        });

        btnCancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                listener.clickCancle();
            }
        });
        mPopupWindow.setTouchable(true);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable(context
                .getResources(), (Bitmap) null));
        mPopupWindow.showAtLocation(contentView, Gravity.NO_GRAVITY, 0, 0);
    }
}
